package com.example.shopspring.controller;


import com.example.shopspring.common.ApiConstant;
import com.example.shopspring.common.ResultData;
import com.example.shopspring.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Slf4j
@RestController
@RequestMapping(ApiConstant.API_VERSION)
public class UploadController {

    /**
     * 注入AliOSSUtils
     */
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @PostMapping("/upload")
    public ResultData txt(@RequestParam(value = "username", required = false) String username,
                          @RequestParam(value = "image") MultipartFile image,
                          @RequestParam(value = "age", required = false) Integer age) throws IOException {
        String url = aliOSSUtils.upload(image);
        return ResultData.success(url);
    }
}
